package io.renren.modules.app.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.dto.DischargeRegistrationDto;
import io.renren.modules.app.dto.ListDto;
import io.renren.modules.app.dto.PatientDto;
import io.renren.modules.app.dto.WardRoundReportDto;
import io.renren.modules.app.interceptor.AuthorizationInterceptor;
import io.renren.modules.app.vo.AllReportVo;
import io.renren.modules.bs.entity.BsDepartmentAdministratorEntity;
import io.renren.modules.bs.service.BsDepartmentAdministratorService;
import io.renren.modules.bs.service.BsDepartmentPatientImgService;
import io.renren.modules.bs.service.BsDepartmentPatientService;
import io.renren.modules.bs.service.BsPatientService;
import io.renren.modules.bs.vo.BsDepartmentPatientPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/app/patient")
@Api(value = "患者controller",tags = "患者controller")
@Slf4j
public class AppPatientController {

    @Autowired
    private BsDepartmentPatientService departmentPatientService;

    @Autowired
    private BsDepartmentAdministratorService administratorService;

    @Autowired
    private BsPatientService patientService;

    @Autowired
    private BsDepartmentPatientImgService imgService;

    @PostMapping("savePatient")
    @ApiOperation("录入信息")
    private R savePatient( @RequestBody PatientDto patientDto){

        this.patientService.savePatient(patientDto);
        return R.ok();
    }

//    @GetMapping("getById")
//    @ApiOperation("根据ID获取信息")
//    private R getById( Long departmentPatientId){
//
//        BsDepartmentPatientVo departmentPatientVo = this.patientService.getModelById(departmentPatientId);
//        return R.ok().put(departmentPatientVo);
//    }

//    BsDepartmentPatientPageVo
    @PostMapping("updateById")
    @ApiOperation("根据ID更新信息")
    private R updateById( @RequestBody BsDepartmentPatientPageVo departmentPatientPageVo){

        this.patientService.updateModelById(departmentPatientPageVo);
        return R.ok();
    }


    @PostMapping("dischargeRegistration")
    @ApiOperation("出院登记")
    private R dischargeRegistration( @RequestBody DischargeRegistrationDto patientDto){

        this.patientService.dischargeRegistration(patientDto);
        return R.ok();
    }

    @GetMapping("getList")
    @ApiOperation("住院人员列表")
    private R getList(HttpServletRequest request, ListDto listDto){

        Long adminId = (Long) request.getAttribute(AuthorizationInterceptor.USER_KEY);
        System.out.println("adminId===========>"+adminId);
        //查询所在科室
        BsDepartmentAdministratorEntity departmentAdministratorEntity = this.administratorService.getById(adminId);
        Long departmentId = departmentAdministratorEntity.getDepartmentId();
        Map<String, Object> params = new HashMap<>();
        params.put("isLeave",0);
        params.put("page",String.valueOf(listDto.getPage()));
        params.put("limit",String.valueOf(listDto.getLimit()));
        params.put("departmentId",departmentId);
        params.put("fromType","app");
        PageUtils pageUtils = this.departmentPatientService.queryPage(params);
        return R.ok().put(pageUtils);
    }

//    @GetMapping("getImgListById")
//    @ApiOperation("根据ID获取图片列表")
//    private R getImgListById(ListDto listDto){
//
//
//        Map<String, Object> params = new HashMap<>();
//        params.put("page",String.valueOf(listDto.getPage()));
//        params.put("limit",String.valueOf(listDto.getLimit()));
//        params.put("department_patient_id",listDto.getDepartmentPatientId());
//        PageUtils pageUtils = this.imgService.queryPage(params);
//        return R.ok().put(pageUtils);
//    }

    @PostMapping("wardRoundReport")
    @ApiOperation("查房上报")
    private R wardRoundReport( @RequestBody WardRoundReportDto wardRoundReportDto){

        this.patientService.wardRoundReport(wardRoundReportDto);
        return R.ok();
    }

    @GetMapping("getReportList")
    @ApiOperation("获得待上报列表")
    private R getReportList(HttpServletRequest request){
        Long adminId = (Long) request.getAttribute(AuthorizationInterceptor.USER_KEY);
        //查询所在科室
        BsDepartmentAdministratorEntity departmentAdministratorEntity = this.administratorService.getById(adminId);
        Long departmentId = departmentAdministratorEntity.getDepartmentId();

        AllReportVo allReportVo = this.patientService.getReportList(departmentId);
        return R.ok().put(allReportVo);
    }


}
